#!/bin/sh
# shellcheck shell=dash disable=SC2169 source=/dev/null
#
# Start all init scripts in /etc/init.d
# executing them in numerical order.

# make sure we have a secure umask
umask 0002

# perform systemwide fsck
/sbin/fsck -A -R -p

# mount all filesystems
/bin/mount -a

# create /var/run and /var/cache
/bin/mkdir -p /var/run /var/cache

# create ld.so.cache file
/sbin/ldconfig -C /var/cache/ld.so.cache

# start psplash
/usr/bin/psplash -n 2>/dev/null &

for i in /etc/init.d/S??* ;do

  # Ignore dangling symlinks (if any).
  [[ ! -f "${i}" ]] && continue

  # put the psplash progess bar to the
  # corresponding level
  name=${i##*/}
  num=${name:1:2}
  /usr/bin/psplash-write "MSG Starting ${name:3}..."
  /usr/bin/psplash-write "PROGRESS $((${num#0}+1))"

  case "${i}" in
    *.sh)
      # Source shell script for speed.
      (
        trap - INT QUIT TSTP
        set start
        . "${i}"
      )
      ;;
    *)
      # No sh extension, so fork subprocess.
      ${i} start
    ;;
  esac
done

IP=$(ip -4 route get 1 | head -1 | cut -d' ' -f8 | tr -d '\n')
/usr/bin/psplash-write "PROGRESS 0"

[[ -r /VERSION ]] && . /VERSION
[[ -r /var/hm_mode ]] && . /var/hm_mode

echo "Finished Boot: ${VERSION} (${PRODUCT})"

MSG="${VERSION} --"
if [[ "${HM_MODE}" == "NORMAL" ]]; then
  if [[ -z "${IP}" ]]; then
    MSG="${MSG} ERROR: No IP address set"
  else
    MSG="${MSG} http://${IP}/"
  fi
else
  if [[ -z "${IP}" ]]; then
    MSG="${MSG} LAN-Gateway Mode (ERROR: No IP address set)"
  else
    MSG="${MSG} LAN-Gateway Mode (IP: ${IP}, Serial: ${HM_HMRF_SERIAL})"
  fi
fi
MSG=$(echo -en "${MSG}\n\n-- Press ALT+F2 for service console")

/usr/bin/psplash-write "MSG ${MSG}"

# quit psplash after 5 seconds
(sleep 5; /usr/bin/psplash-write "QUIT") &
